import sys
import os
import time

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import boto3
from config import *
from pyathena import connect
import json


client = boto3.client('athena')


def submit(sql):
    result = try_submit(sql)
    if result["statusCode"] == 500:
        for i in range(50):
            time.sleep(1)
            result = try_submit(sql)
    return result



def try_submit(sql):
    try:
        cursor = connect(s3_staging_dir=OutputLocation,
                         region_name="cn-northwest-1").cursor(schema_name=db)

        cursor.execute(sql)
        cursor.fetchall()
        return  {
            'statusCode': 200,
            'body': json.dumps('success')
        }
    except Exception as e:
       return {
            'statusCode': 500,
            'body': json.dumps('error'),
            'msg':e
        }


